<%@ include file="/common/taglibs.jsp"%>

<head>
    <title><fmt:message key="dealerDetail.title"/></title>
    <meta name="menu" content="DealerMenu"/>
    <meta name="heading" content="<fmt:message key='dealerDetail.heading'/>"/>

</head>

<c:set var="delObject" scope="request"><fmt:message key="dealerList.dealer"/></c:set>
<script type="text/javascript">var msgDelConfirm =
   "<fmt:message key="delete.confirm"><fmt:param value="${delObject}"/></fmt:message>";
</script>
<section class="content-header">
    <h1>
        <fmt:message key="dealerDetail.heading"/>
    </h1>
    <ol class="breadcrumb">
        <li><a href="/loanRequests"><i class="fa fa-dashboard"></i><fmt:message key="dealerDetail.heading"/></a></li>
        <li class="active"><fmt:message key="dealerDetail.heading"/></li>
    </ol>
</section>
<!-- Main content -->
<section class="content">
    <div class="row">
        <div class="col-md-12">
            <div class="box box-primary">
                <div class="box-header with-border">
                    <h3 class="box-title"><fmt:message key="dealerDetail.heading"/></h3>
                </div>
                <div class="box-body">
                    <%@ include file="/common/messages.jsp" %>
                    <form:errors path="dealer.*" cssClass="alert alert-danger alert-dismissable" element="div"/>
                    <form:form commandName="dealer" method="post" action="dealerform"
                               id="dealerForm" onsubmit="return validateDealer(this)">
                    <div class="form-horizontal margin well">
                            <form:hidden path="id"/>
                        <form:hidden path="bond.id"/>
                        <form:hidden path="adminUser.id"/>
                        <form:hidden path="adminUser.version"/>
                        <input hidden name="bondId" value="${param.bondId}"/>
                        <input type="hidden" name="from" value="<c:out value="${param.from}"/>"/>

                        <div class="form-group">
                            <spring:bind path="dealer.name">
                                <appfuse:label key="dealer.name" styleClass="control-label col-sm-1"/>
                                <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                    </spring:bind>
                            <form:input cssClass="form-control" path="name" id="name"  maxlength="255"/>
                            <form:errors path="name" cssClass="help-block"/>
                            </div>
                                <spring:bind path="dealer.bond.name">
                                <appfuse:label key="dealer.bondCompany" styleClass="control-label col-sm-1"/>
                                <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                    </spring:bind>
                                    <form:input cssClass="form-control" path="bond.name" id="bond.name"  maxlength="255" readonly="true"/>
                                    <form:errors path="bond.name" cssClass="help-block"/>
                                </div>
                                <spring:bind path="dealer.plateRestricted">
                                <appfuse:label key="dealer.plateRestricted" styleClass="control-label col-sm-1"/>
                                <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                    </spring:bind>
                                    <form:checkbox path="plateRestricted" id="plateRestricted" />
                                    <form:errors path="plateRestricted" cssClass="help-block"/>
                                </div>
                        </div>
                        <div class="form-group">
                                <spring:bind path="dealer.riskRank">
                                <appfuse:label key="dealer.riskRank" styleClass="control-label col-sm-1"/>
                                <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                    </spring:bind>
                                    <form:select cssClass="form-control" path="riskRank" id="riskRank">
                                        <option value=""><fmt:message key="option.select"/></option>
                                        <form:options items="${availableRiskRanks}" itemValue="value" itemLabel="label"/>
                                    </form:select>
                                    <form:errors path="riskRank" cssClass="help-block"/>
                                </div>
                                    <spring:bind path="dealer.allowNoMortgaged">
                                    <appfuse:label key="dealer.allowNoMortgaged" styleClass="control-label col-sm-1"/>
                                    <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                        </spring:bind>
                                        <form:checkbox path="allowNoMortgaged" id="allowNoMortgaged" />
                                        <form:errors path="allowNoMortgaged" cssClass="help-block"/>
                                    </div>
                                    <spring:bind path="dealer.insuranceChannel">
                                    <appfuse:label key="dealer.insuranceChannel" styleClass="control-label col-sm-1"/>
                                    <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                        </spring:bind>
                                        <form:checkbox path="insuranceChannel" id="insuranceChannel" />
                                        <form:errors path="insuranceChannel" cssClass="help-block"/>
                                    </div>
                        </div>
                        <div class="form-group">
                            <spring:bind path="dealer.adminUser.username">
                            <appfuse:label key="admin.username" path="dealer.adminUser.username" styleClass="control-label col-sm-1"/>
                            <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                </spring:bind>
                                <form:input cssClass="form-control" path="adminUser.username" id="adminUser.username"  maxlength="255"/>
                                <form:errors path="adminUser.username" cssClass="help-block"/>
                            </div>
                                <spring:bind path="dealer.adminUser.password">
                                <appfuse:label key="user.password" path="dealer.adminUser.password" styleClass="control-label col-sm-1"/>
                                <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                    </spring:bind>
                                    <form:password cssClass="form-control" path="adminUser.password" id="adminUser.password"  maxlength="255" showPassword="true"/>
                                    <form:errors path="adminUser.password" cssClass="help-block"/>
                                </div>
                                <spring:bind path="dealer.adminUser.passwordHint">
                                <appfuse:label key="user.passwordHint" path="dealer.adminUser.passwordHint" styleClass="control-label col-sm-1"/>
                                <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                    </spring:bind>
                                    <form:input cssClass="form-control" path="adminUser.passwordHint" id="adminUser.passwordHint"  maxlength="255"/>
                                    <form:errors path="adminUser.passwordHint" cssClass="help-block"/>
                                </div>
                        </div>
                        <div class="form-group">
                            <spring:bind path="dealer.adminUser.name">
                            <appfuse:label key="dealer.contactPerson" path="dealer.adminUser.name" styleClass="control-label col-sm-1"/>
                            <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                </spring:bind>
                                <form:input cssClass="form-control" path="adminUser.name" id="adminUser.name"  maxlength="255"/>
                                <form:errors path="adminUser.name" cssClass="help-block"/>
                            </div>
                            <spring:bind path="dealer.adminUser.phoneNumber">
                            <appfuse:label key="dealer.contactNumber" path="dealer.adminUser.phoneNumber" styleClass="control-label col-sm-1"/>
                            <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                </spring:bind>
                                <form:input cssClass="form-control" path="adminUser.phoneNumber" id="adminUser.phoneNumber"  maxlength="255"/>
                                <form:errors path="adminUser.phoneNumber" cssClass="help-block"/>
                            </div>
                                <spring:bind path="dealer.adminUser.email">
                                <appfuse:label key="user.email" path="dealer.adminUser.email" styleClass="control-label col-sm-1"/>
                                <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                    </spring:bind>
                                    <form:input cssClass="form-control" path="adminUser.email" id="adminUser.email"  maxlength="255"/>
                                    <form:errors path="adminUser.email" cssClass="help-block"/>
                                </div>

                        </div>
                        <div class="form-group">
                            <spring:bind path="dealer.province">
                                <appfuse:label key="dealer.province" styleClass="control-label col-sm-1"/>
                                <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                            </spring:bind>
                            <input value="${dealer.province}" id="provinceValue" hidden/>
                            <form:select cssClass="form-control" path="province" id="province">
                                <option value=""><fmt:message key="option.select"/></option>
                                <form:options items="${availableProvinces}" itemValue="value" itemLabel="label"/>
                            </form:select>
                            <form:errors path="province" cssClass="help-block"/>
                            </div>
                            <spring:bind path="dealer.city">
                                <appfuse:label key="dealer.city" styleClass="control-label col-sm-1"/>
                                <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                            </spring:bind>
                            <input value="${dealer.city}" id="cityValue" hidden/>
                            <select class="form-control" name="city" id="city" >
                                <option value=""><fmt:message key="option.city"/></option>
                            </select>
                            <form:errors path="city" cssClass="help-block"/>
                            </div>
                                <spring:bind path="dealer.mailAddress">
                                <appfuse:label key="dealer.mailAddress" styleClass="control-label col-sm-1"/>
                                <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                    </spring:bind>
                                    <form:input cssClass="form-control" path="mailAddress" id="mailAddress"  maxlength="255"/>
                                    <form:errors path="mailAddress" cssClass="help-block"/>
                                </div>
                        </div>
                        <div class="form-group">
                            <spring:bind path="dealer.bank">
                            <appfuse:label key="dealer.bank" styleClass="control-label col-sm-1"/>
                            <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                </spring:bind>
                                    <form:input cssClass="form-control" path="bank" id="bank"  maxlength="255"/>
                                    <form:errors path="bank" cssClass="help-block"/>
                            </div>
                            <spring:bind path="dealer.bankAccount">
                            <appfuse:label key="dealer.bankAccount" styleClass="control-label col-sm-1"/>
                            <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                </spring:bind>
                                    <form:input cssClass="form-control" path="bankAccount" id="bankAccount"  maxlength="255"/>
                                    <form:errors path="bankAccount" cssClass="help-block"/>
                            </div>
                            <spring:bind path="dealer.cardName">
                            <appfuse:label key="dealer.cardName" styleClass="control-label col-sm-1"/>
                            <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                </spring:bind>
                                <form:input cssClass="form-control" path="cardName" id="cardName"  maxlength="255"/>
                                <form:errors path="cardName" cssClass="help-block"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <spring:bind path="dealer.slaveProviderId">
                            <appfuse:label key="dealer.slaveProviderId" styleClass="control-label col-sm-1"/>
                                <input hidden value="${dealer.slaveProvider.name}" id="slaveProviderId"/>
                            <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                </spring:bind>
                                    <form:select path="slaveProviderId" cssClass="form-control" id="slaveProvider">
                                        <option value=""><fmt:message key="option.select"/></option>
                                        <form:options items="${availableSlaveProviders}" itemValue="value" itemLabel="label"/>
                                    </form:select>
                                    <form:errors path="slaveProviderId" cssClass="help-block"/>
                            </div>
                            <spring:bind path="dealer.masterProviderId">
                            <appfuse:label key="dealer.masterProviderId" styleClass="control-label col-sm-1"/>
                                <input hidden value="${dealer.masterProvider.name}" id="masterProviderId"/>
                            <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                </spring:bind>
                                    <form:select path="masterProviderId" cssClass="form-control" id="masterProvider">
                                        <option value=""><fmt:message key="option.select"/></option>
                                        <form:options items="${availableMasterProviders}" itemValue="value" itemLabel="label"/>
                                    </form:select>
                                <form:errors path="masterProviderId" cssClass="help-block"/>
                            </div>
                            <spring:bind path="dealer.premiumRate">
                            <appfuse:label key="dealer.premiumRate" styleClass="control-label col-sm-1"/>
                            <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                </spring:bind>
                                <form:input cssClass="form-control" path="premiumRate" id="premiumRate"  maxlength="255"/>
                                <form:errors path="premiumRate" cssClass="help-block"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <spring:bind path="dealer.requestEnabled">
                            <appfuse:label key="dealer.requestEnabled" styleClass="control-label col-sm-1"/>
                            <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                </spring:bind>
                                    <form:checkbox path="requestEnabled" id="requestEnabled" />
                                    <form:errors path="requestEnabled" cssClass="help-block"/>
                            </div>

                        </div>
                        <div class="form-group">
                            <spring:bind path="dealer.loanEnabled">
                            <appfuse:label key="dealer.loanEnabled" styleClass="control-label col-sm-1"/>
                            <div class="col-sm-3${(not empty status.errorMessage) ? ' has-error' : ''}">
                                </spring:bind>
                                <form:checkbox path="loanEnabled" id="loanEnabled" />
                                <form:errors path="loanEnabled" cssClass="help-block"/>
                            </div>
                        </div>
                        <div id="actions" class="form-group">
                            <button type="submit" class="btn btn-primary" id="save" name="save" onclick="bCancel=false">
                                <i class="icon-ok icon-white"></i> <fmt:message key="button.save"/>
                            </button>
                            <c:if test="${not empty dealer.id}">
                                <button type="submit" class="btn btn-danger" id="delete" name="delete" onclick="bCancel=true;return confirmMessage(msgDelConfirm)">
                                    <i class="icon-trash icon-white"></i> <fmt:message key="button.delete"/>
                                </button>
                            </c:if>
                            <button type="submit" class="btn btn-default" id="cancel" name="cancel"
                                    onclick="bCancel=true">
                                <i class="icon-remove"></i> <fmt:message key="button.cancel"/>
                            </button>
                        </div>

                    </div>
                    <br/>
                    <display:table name="userList" cellspacing="0" cellpadding="0" requestURI=""
                                   defaultsort="1" id="users" pagesize="25" class="table table-condensed table-striped table-hover" export="true">
                        <display:column property="username" escapeXml="true" sortable="true" titleKey="user.username" style="width: 20%"
                                        url="/userform?from=dealer&dealerId=${dealer.id}" paramId="id" paramProperty="id"></display:column>
                        <display:column property="name" escapeXml="true" sortable="true" titleKey="activeUsers.name"
                                        style="width: 20%"/>
                        <display:column sortable="false" titleKey="user.roles"
                                        style="width: 20%; padding-left: 15px" media="html">
                            <c:forEach var="role" items="${users.roleList}" varStatus="status">
                                <fmt:message key="${role.label}"/><c:if test="${!status.last}">,</c:if>
                            </c:forEach>
                        </display:column>
                        <display:column property="email" sortable="true" titleKey="user.email" style="width: 20%" autolink="true"
                                        media="html"/>
                        <display:column property="email" titleKey="user.email" media="csv xml excel pdf"/>
                        <display:column sortProperty="enabled" sortable="true" titleKey="user.enabled"
                                        style="width: 15%; padding-left: 15px" media="html">
                            <input type="checkbox" disabled="disabled" <c:if test="${users.enabled}">checked="checked"</c:if>/>
                        </display:column>
                        <display:column property="enabled" titleKey="user.enabled" media="csv xml excel pdf"/>

                        <display:setProperty name="paging.banner.item_name"><fmt:message key="userList.user"/></display:setProperty>
                        <display:setProperty name="paging.banner.items_name"><fmt:message key="userList.users"/></display:setProperty>

                        <display:setProperty name="export.excel.filename" value="User List.xls"/>
                        <display:setProperty name="export.csv.filename" value="User List.csv"/>
                        <display:setProperty name="export.pdf.filename" value="User List.pdf"/>
                    </display:table>

                    </form:form>

                </div>
            </div>
        </div>
    </div>

</section>
<c:if test="${not empty dealer.id}">
<section class="content">
    <div class="row">
        <div class="col-md-12">
            <div class="box box-primary">
                <div class="box-header with-border">
                    <h3 class="box-title"><fmt:message key="dealerRule.heading"/></h3>
                </div>
                <div id="ruleActions" class="btn-group margin">
                    <a href="/dealerRuleForm?from=list&dealerId=${dealer.id}" class="btn btn-primary" id="addDealerRule">
                        <i class="fa fa-plus"></i><fmt:message key="ACTION_addDealerRule"/></a>
                </div>
                <div class="box-body table-responsive">
                    <display:table name="dealerRuleList" class="table table-condensed table-striped table-hover" requestURI=""
                                   id="dealerRules" export="false" pagesize="25">
                        <display:column property="code" sortable="true" href="dealerRuleForm?from=list" media="html"
                                        paramId="dealerRuleId" paramProperty="id" titleKey="dealerRule.code"/>
                        <display:column property="id" media="csv excel xml pdf" titleKey="dealerRule.id"/>
                        <display:column property="description" sortable="true" titleKey="dealerRule.description"/>
                        <display:column sortable="true" titleKey="dealerRule.status">
                            <input type="checkbox" disabled="disabled" <c:if test="${dealerRules.status}">checked="checked"</c:if>/>
                        </display:column>
                        <display:setProperty name="paging.banner.item_name"><fmt:message key="dealerRule.dealerRule"/></display:setProperty>
                        <display:setProperty name="paging.banner.items_name"><fmt:message key="dealerRule.dealerRule"/></display:setProperty>
                    </display:table>

                </div><!-- /.box-body -->
            </div>
        </div>
    </div>
</section>
</c:if>



<c:set var="scripts" scope="request">
<v:javascript formName="dealer" cdata="false" dynamicJavascript="true" staticJavascript="false"/>
<script type="text/javascript" src="<c:url value='/scripts/validator.jsp'/>"></script>
<script type='text/javascript' src="<c:url value='/dwr/interface/ProvinceManager.js'/>"></script>
<script type='text/javascript' src="<c:url value='/dwr/interface/CityManager.js'/>"></script>
<script type='text/javascript' src="<c:url value='/dwr/engine.js'/>"></script>
<script type='text/javascript' src="<c:url value='/dwr/util.js'/>"></script>
<script type="text/javascript">

    $(document).ready(function() {
        $( "#slaveProvider" ).combobox();
        $("#masterProvider").combobox();
        $("input[type='text']:visible:enabled:first", document.forms['dealerForm']).focus();
        if( $('#provinceValue').val() ){
            dwr.util.setValue('province', $('#provinceValue').val());
            var prov_id = dwr.util.getValue('province');
            ProvinceManager.getMyCity(prov_id, function(data){
                dwr.util.addOptions('city', data,  "name");
                dwr.util.setValue('city',$('#cityValue').val());
//                var city_id = $('#cityValue').val();
//                if (city_id){
//                    CityManager.getMyCounty(city_id, function(data){
//                        dwr.util.addOptions('county', data, "name");
//                        dwr.util.setValue('county',$('#countyValue').val());
//                    })
//                }
            })
        }
        $('#province').change(function(){
            $('#city option').remove();
            var prov_id = dwr.util.getValue('province');
            ProvinceManager.getMyCity(prov_id, function(data){
                dwr.util.addOptions('city', data,  "name");
            })
        });

        if($('#masterProviderId').val()!=null){
            $("#masterProvider").combobox('autocomplete',$('#masterProviderId').val());
        }
        if($('#slaveProviderId').val()!=null){
            $("#slaveProvider").combobox('autocomplete', $('#slaveProviderId').val());
        }

    });
</script>
</c:set>
